<template>
  <van-icon
    color="#777"
    :name="value ? 'star' : 'star-o'"
    :class="{ collected: value }"
    @click="onClick"
  />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  props: {
    value: {
      type: Boolean,
      require: true
    },
    articleId: {
      type: [Number, String, Object],
      require: true
    }
  },

  components: {},
  data () {
    return {
      loading: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onClick () {
      try {
        if (this.loading) return
        this.loading = true
        if (this.value) {
          // 取消
          await deleteCollect(this.articleId)
        } else {
          // 收藏
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(this.value ? '取消收藏' : '收藏成功')
        this.loading = false
      } catch (error) {
        this.$toast('操作失败，请重试')
      }
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
.collected {
  color: #ffa500 !important;
}
</style>
